package com.hongxun.project.assets.bByq.controller;

import java.util.*;

import com.hongxun.common.utils.text.Convert;
import com.hongxun.framework.web.domain.Ztree;
import com.hongxun.project.assets.Tools.StringZTree;
import com.hongxun.project.assets.iDevice.service.IIDeviceService;
import com.hongxun.project.assets.iLine.domain.ILine;
import com.hongxun.project.assets.iLine.service.IILineService;
import com.hongxun.project.assets.iPoint.domain.IPoint;
import com.hongxun.project.assets.iPoint.service.IIPointService;
import com.hongxun.project.assets.pDevice.service.IPDeviceService;
import com.hongxun.project.assets.pPoint.service.IPPointService;
import com.hongxun.project.system.dept.domain.Dept;
import com.hongxun.project.system.dept.service.IDeptService;
import com.hongxun.project.system.role.domain.Role;
import com.hongxun.project.system.role.domain.RoleDept;
import com.hongxun.project.system.role.mapper.RoleMapper;
import com.hongxun.project.system.role.service.IRoleService;
import com.hongxun.project.system.user.domain.User;
import com.hongxun.project.system.user.domain.UserRole;
import com.hongxun.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hongxun.framework.aspectj.lang.annotation.Log;
import com.hongxun.framework.aspectj.lang.enums.BusinessType;
import com.hongxun.project.assets.bByq.domain.BByq;
import com.hongxun.project.assets.bByq.service.IBByqService;
import com.hongxun.framework.web.controller.BaseController;
import com.hongxun.framework.web.page.TableDataInfo;
import com.hongxun.framework.web.domain.AjaxResult;
import com.hongxun.common.utils.poi.ExcelUtil;

/**
 * 变压器 信息操作处理
 * 
 * @author hongxun
 * @date 2019-03-19
 */
@Controller
@RequestMapping("/assets/bByq")
public class BByqController extends BaseController
{
    private String prefix = "assets/bByq";
	
	@Autowired
	private IBByqService bByqService;
	@Autowired
	private IIPointService iPointService;
	@Autowired
	private IILineService iLineService;
	@Autowired
	private IPDeviceService pDeviceService;
	@Autowired
	private IIDeviceService iDeviceService;
	@Autowired
	private IDeptService deptService;
	@Autowired
	private IUserService userService;
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IPPointService pPointService;

	
	@RequiresPermissions("assets:bByq:view")
	@GetMapping()
	public String bByq()
	{
	    return prefix + "/bByq";
	}

	/**
	 * 变压器列表选择
	 * @return
	 */
	@GetMapping("/chooseByq")
	public String chooseByq()
	{
	    return prefix + "/chooseByq";
	}

	@GetMapping("/iframeDxf/{id}")
	public String iframeDxf(@PathVariable("id") String id, ModelMap mmap)
	{
		mmap.put("id",id);
	    return prefix + "/iframeDxf";
	}

	/**
	 * 	单线图显示
	 *
	 * @param
	 * @param mmap
	 * @return
	 */
	@GetMapping("/showDxf/{byq_id}")
	public String showDxf(@PathVariable("byq_id") String byq_id, ModelMap mmap)
	{
		BByq bByq = bByqService.selectBByqById(byq_id);
		mmap.put("byq_id",byq_id);
		mmap.put("byq_name",bByq.getName());
	    return prefix + "/showDxf";
	}


	/**
	 * 变压器名称回显
	 * @param
	 * @return
	 */
	@PostMapping("/getByqName")
	@ResponseBody
	public Map<String , Object> getByqName(BByq bByq)
	{
		Map<String , Object> newMap = new HashMap<>();
		List<BByq> bByqInfo = bByqService.selectBByqList(bByq);
		newMap.put("bByqList", bByqInfo);
		return newMap;
	}

	/**
	 * 查询变压器列表
	 */
	@RequiresPermissions("assets:bByq:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(BByq bByq)
	{

		startPage();
		List<BByq> bByqList = new ArrayList<>();
		if(getUserId() !=1) {
			String name = bByq.getName();
			Date runDate = bByq.getRunDate();
			bByqList = bByqService.selectByqToUserId(getUserId(),name,runDate);
		}else
			bByqList = bByqService.selectBByqList(bByq);

		return getDataTable(bByqList);
	}
	
	
	/**
	 * 导出变压器列表
	 */
	@RequiresPermissions("assets:bByq:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BByq bByq)
    {
    	List<BByq> list = bByqService.selectBByqList(bByq);
        ExcelUtil<BByq> util = new ExcelUtil<BByq>(BByq.class);
        return util.exportExcel(list, "bByq");
    }
	
	/**
	 * 新增变压器
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存变压器
	 */
	@RequiresPermissions("assets:bByq:add")
	@Log(title = "变压器", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(BByq bByq)
	{
		bByq.setAddUser(getUserId().intValue());
		bByq.setAddDate(new Date());

		return toAjax(bByqService.insertBByq(bByq));
	}

	/**
	 * 修改变压器
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
        BByq bByq = new BByq();
        if(iPoint == null){
		    bByq = bByqService.selectBByqById(id);
			Dept dept = deptService.selectDeptById(Long.parseLong(bByq.getDeptId()));
			bByq.setDeptName(dept.getDeptName());
        }else{
            bByq = bByqService.selectBByqById(iPoint.getByqId());
        }
		mmap.put("bByq", bByq);
	    return prefix + "/edit";
	}

	/**
	 * 	任务详情--变压器点位修改
	 *
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/editss/{id}")
	public String editss(@PathVariable("id") String id, ModelMap mmap)
	{
		IPoint iPoint = iPointService.selectIPointById(id);
		BByq bByq = bByqService.selectBByqById(iPoint.getByqId());
		Dept dept = deptService.selectDeptById(Long.parseLong(bByq.getDeptId()));
		bByq.setDeptName(dept.getDeptName());
		mmap.put("bByq", bByq);
	    return prefix + "/editss";
	}
	
	/**
	 * 修改保存变压器
	 */
	@RequiresPermissions("assets:bByq:edit")
	@Log(title = "变压器", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(BByq bByq)
	{
		bByq.setUpdateUser(getUserId().intValue());
		bByq.setUpdateDate(new Date());
		return toAjax(bByqService.updateBByq(bByq));
	}

	@RequiresPermissions("assets:bByq:edit")
	@Log(title = "变压器", businessType = BusinessType.UPDATE)
	@PostMapping("/editss")
	@ResponseBody
	public AjaxResult editSaves(BByq bByq)
	{
		bByq.setUpdateUser(getUserId().intValue());
		bByq.setUpdateDate(new Date());
		return toAjax(bByqService.updateBByq(bByq));
	}
	
	/**
	 * 删除变压器
	 */
	@RequiresPermissions("assets:bByq:remove")
	@Log(title = "变压器", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		String[] idArr = Convert.toStrArray(ids);
		List<ILine> iLines = new ArrayList<>();
		for (String s : idArr) {
			ILine iLine = new ILine();
			iLine.setByqId(s);
			iLines = iLineService.selectILineList(iLine);
			if (iLines.size() != 0 ){
				return error("该变压器下有线路信息，无法删除！");
			}
		}

		return toAjax(bByqService.deleteBByqByIds(ids));
	}



	/**
	 * 加载所有变压器列表树
	 */
	@GetMapping("/byqTreeData")
	@ResponseBody
	public List<StringZTree> byqTreeData(BByq bByq , @RequestParam Map<String , Object> objectMap)
	{
		// 当前显示页
		int page = Integer.parseInt(objectMap.get("page").toString());
		String byqName = objectMap.get("byqName").toString();
		List<StringZTree> ztrees = new ArrayList<>();
		if(byqName.equals("")){
			ztrees = bByqService.byqTreeData(byqName , page);
		}else{
			bByq.setName(byqName);
			ztrees = bByqService.byqTreeData(byqName , page);
		}
//		List<StringZTree> ztrees = bByqService.byqTreeData(bByq );
		return ztrees;
	}


	/**
	 * 	资产概览--是否需要
	 *
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/showAssets/{id}")
	public String showAssets(@PathVariable("id") String id, ModelMap mmap)
	{
		BByq bByq = bByqService.selectBByqById(id);
//		IPoint iPoint = iPointService.selectIPointList();
		mmap.put("bByq", bByq);
		return prefix + "/edit";
	}
	
}
